<?php
class autocomplete extends CI_Controls{

	public $tags;
	public $min_length;
	public $source;
	public $scrollable=true;
	public $comboBox=false;
	public $data;
	public $campoLink;
	public $campoLabel="label";
	public $campoValue="value";

	function __construct(){
		parent::__construct();
	}

	function drawContent(){
		$texto= new text();
		$texto->atributos=$this->atributos;
		$texto->value=$this->value;
		$this->autocompleteScript();
		return $texto;

	}

	private function autocompleteScript(){

		ob_start();
		?>
			<script>
			var input=$( "#<?php echo $this->att('id');?>" );
			input.autocomplete({
				source: <?php if($this->source){?> function(request,response){	
					$.ajax({
						url:'<?php echo $this->source ?>/'+request.term,
						dataType:'json',
						type:'POST',
						<?php if(!empty($this->data)){?>
						data:<?php echo json_encode($this->data); ?>,
						<?php } ?>
						success: function (data){
							response(data);
						}
					});
				}
					
				<?php } else {
					echo json_encode($tags);
				 } ?>,
				minLength:<?php echo $this->min_length; ?>,
				//appendTo: '#<?php //echo $this->att('id');?>_content',
				select: function(event, ui){
					input.val(ui.item.value);
					input.change();
				}
				
			})
			<?php if(!empty($this->campoLink)){?>
			.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
			      return $( "<li>" )
			        .append( "<a href='"+item.link+"'>" + item.label +"</a>" )
			        .appendTo( ul );
			    };
			<?php } ?>

			</script>
				 <?php
				 	
				 $this->CI->dynamic_assets->add_code_js(ob_get_clean());
				 
				 ob_start();
			?>
			<style>
			.ui-autocomplete {
			    max-height: 500px;
			    overflow-y: auto;
			    /* prevent horizontal scrollbar */
			    overflow-x: hidden;
			}
			</style>
			<?php 
				$this->CI->dynamic_assets->add_code_css(ob_get_clean());
	}
}